package com.irdstudio.efp.flow.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.efp.flow.common.constance.FlowConstance;
import com.irdstudio.efp.flow.service.facade.InstFlowEventService;
import com.irdstudio.efp.flow.service.facade.InstFlowProcessService;
import com.irdstudio.efp.flow.service.vo.InstFlowProcessVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.flow.service.dao.BizEventInfoDao;
import com.irdstudio.efp.flow.service.domain.BizEventInfo;
import com.irdstudio.efp.flow.service.facade.BizEventInfoService;
import com.irdstudio.efp.flow.service.vo.BizEventInfoVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 业务流转事件信息				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizEventInfoService")
public class BizEventInfoServiceImpl extends AbstractFrameworkService implements BizEventInfoService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizEventInfoServiceImpl.class);

	@Autowired
	private BizEventInfoDao bizEventInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertBizEventInfo(BizEventInfoVO inBizEventInfoVo) {
		logger.debug("当前新增数据为:"+ inBizEventInfoVo.toString());
		int num = 0;
		try {
			BizEventInfo bizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, bizEventInfo);
//			bizEventInfo.setBizEventId(this.getSequence(SequenceConstance.BIZ_EVENT_SEQ_ID));
			num = bizEventInfoDao.insertBizEventInfo(bizEventInfo);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizEventInfoVO inBizEventInfoVo) {
		logger.debug("当前删除数据条件为:"+ inBizEventInfoVo);
		int num = 0;
		try {
			BizEventInfo bizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, bizEventInfo);
			num = bizEventInfoDao.deleteByPk(bizEventInfo);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizEventInfoVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizEventInfoVO inBizEventInfoVo) {
		logger.debug("当前修改数据为:"+ inBizEventInfoVo.toString());
		int num = 0;
		try {
			BizEventInfo bizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, bizEventInfo);
			num = bizEventInfoDao.updateByPk(bizEventInfo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizEventInfoVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizEventInfoVO queryByPk(BizEventInfoVO inBizEventInfoVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizEventInfoVo);
		try {
			BizEventInfo queryBizEventInfo = new BizEventInfo();
			beanCopy(inBizEventInfoVo, queryBizEventInfo);
			BizEventInfo queryRslBizEventInfo = bizEventInfoDao.queryByPk(queryBizEventInfo);
			if (Objects.nonNull(queryRslBizEventInfo)) {
				BizEventInfoVO outBizEventInfoVo = beanCopy(queryRslBizEventInfo, new BizEventInfoVO());
				logger.debug("当前查询结果为:"+ outBizEventInfoVo.toString());
				return outBizEventInfoVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllOwner(BizEventInfoVO bizEventInfoVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizEventInfoVO> list = null;
		try {
			List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllOwnerByPage(bizEventInfoVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizEventInfos.size());
			pageSet(bizEventInfos, bizEventInfoVo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllCurrOrg(BizEventInfoVO bizEventInfoVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllCurrOrgByPage(bizEventInfoVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizEventInfos.size());
		List<BizEventInfoVO> list = null;
		try {
			pageSet(bizEventInfos, bizEventInfoVo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllCurrDownOrg(BizEventInfoVO bizEventInfoVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllCurrDownOrgByPage(bizEventInfoVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizEventInfos.size());
		List<BizEventInfoVO> list = null;
		try {
			pageSet(bizEventInfos, bizEventInfoVo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	public int countByFlowIdAndEventId(String flowId, String eventId) {
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("flowId", flowId);
		paramMap.put("eventId", eventId);
		int num = 0;
		try {
			logger.info("根据流程ID:"+ flowId +",事件ID:"+ eventId +"查询开始.....");
			num = this.bizEventInfoDao.countByFlowIdAndEventId(paramMap);
			logger.info("根据流程ID:"+ flowId +",事件ID:"+ eventId +"查询结束.....");
		} catch (Exception e) {
			logger.error("根据流程ID:"+ flowId +",事件ID:"+ eventId +"查询出现异常!", e);
			num = -1;
		}
		return num;
		
	}
	
	/**
	 * 根据业务流转类型查找事件信息
	 */
	@SuppressWarnings("unchecked")
	public List<BizEventInfoVO> queryAllByFlowType(BizEventInfoVO bizEventInfoVo) {
		List<BizEventInfoVO> list = null;
		try {
			BizEventInfo eventInfo = new BizEventInfo();
			beanCopy(bizEventInfoVo, eventInfo);
			List<BizEventInfo> bizEventInfos = bizEventInfoDao.queryAllByFlowType(eventInfo);
			list = (List<BizEventInfoVO>) beansCopy(bizEventInfos, BizEventInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}



	/**
	 * 需求编号：【流程打回调用的公共方法】
	 * 开发人员：【chenxuan】
	 * 创建/修改日期：【2019/9/6】
	 * 修改描述：通过流程后处理更新流程为上一审批节点
	 *
	 * @return void
	 * @Param []
	 **/
	public  boolean isFirstNode(String bizSerno ){
		InstFlowProcessVO lastVo = new InstFlowProcessVO();
		lastVo.setBizSerno(bizSerno);
		lastVo.setInstNodeState(FlowConstance.NODE_STATUS_END);
		lastVo.setApproveOpinion(FlowConstance.PASSCODE);
		// 查询最后一个审批通过的审批节点
		InstFlowProcessService instFlowProcessService = SpringContextUtils.getBean(InstFlowProcessService.class);
		InstFlowEventService instFlowEventService = SpringContextUtils.getBean(InstFlowEventService.class);
		List<InstFlowProcessVO> lastInstFlowProcessVOS = instFlowProcessService.queryAllByBizSernoAndConditon(lastVo);
		// 存在上一审批节点处理
		if (lastInstFlowProcessVOS.size() != 0) {
			InstFlowProcessVO lastInstFlowProcessVO  = lastInstFlowProcessVOS.get(0);
			String instNodeId = lastInstFlowProcessVOS.get(0).getInstNodeId();
			InstFlowProcessVO curVo = new InstFlowProcessVO();
			curVo.setBizSerno(bizSerno);
			curVo.setProcessState(FlowConstance.PROCESS_STATE_FINISH);
			curVo.setInstNodeState(FlowConstance.NODE_STATUS_END);
			curVo.setApproveOpinion(FlowConstance.REPULSECODE);
			List<InstFlowProcessVO> curInstFlowProcessVOS = instFlowProcessService.queryAllByBizSernoAndConditon(curVo);
			// 查询最后一个打回节点
			if (null != curInstFlowProcessVOS || curInstFlowProcessVOS.size() != 0) {
				lastInstFlowProcessVO.setProcessState(FlowConstance.PROCESS_STATE_WAIT);
				lastInstFlowProcessVO.setInstNodeState("03");
				int num = instFlowProcessService.updateAprvUserInfoByNodeInstId(lastInstFlowProcessVO);
				if (num != 1)
					throw new RuntimeException("节点状态更新失败");
			}
			int num = instFlowEventService.updateInstNodeIdByBizSerno(bizSerno,instNodeId);
			if (num!=1)
				throw new RuntimeException("事件节点更新失败");
			return false;
		}else{
			return true;
		}
	}
}
